// miniprogram/pages/order/task/add.js
import dayjs from "dayjs"
import U from "../../../utils/index"
import WxValidate from "../../../utils/vendor/WxValidate"
import {IO} from "../../../utils/vendor/ramda-fantasy"
import * as R from "ramda"
const validator = {
  rules: {
    task_name: {
      required: true,
    },
    task_date: {
      required: true,
    },
  },
  messages: {
    task_name: {
      required: '请输入任务名',
    },
    task_date: {
      required: '请选择任务预计完成时间',
    },
  },
}
const wxValidate = new WxValidate(validator.rules, validator.messages);

const requestCreateForm = U.reqPostShow('/task/add_task', '新增任务成功！');

const handleSuccess = () => IO(
  //延迟返回是为了有时间显示提示
  () => setTimeout(()=>{
    wx.navigateBack()
  }, 1500)
);

Page({

  /**
   * 页面的初始数据
   */
  data: {
    form: {},
    aliasForm: {},
    datePickerData: {
      isShowDoneDate: false,
    },
    currentDate: new Date().getTime(),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ['form.order_id']: Number(options.orderId),
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  setFormData(e) {
    U.setFormData(e).run(this);
  },
  switchDoneDate() {
    this.setData({
      [ 'datePickerData.isShowDoneDate' ]: !this.data.datePickerData.isShowDoneDate,
    })
  },
  onConfirmDoneDate(e) {
    this.setData({
      [ 'form.task_date' ]: dayjs(e.detail).format('YYYY-MM-DD'),
      [ 'aliasForm.task_date' ]: e.detail,
      [ 'datePickerData.isShowDoneDate' ]: false,
    })
  },
  onSubmit() {
    R.compose(
      U.runIO,
      U.handleError,
      R.map(
        R.compose(
          U.fork,
          R.map(R.compose(U.runIO, U.backReqSuc)),
          requestCreateForm
        ),
      ),
      U.validate(wxValidate),
      R.tap(form => this.setData({ form })),
      R.map(U.trim),
    )(this.data.form);
  },
})
